
from fastapi import FastAPI, APIRouter, Request, Depends
from starlette import status
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
from controller.{{Lower .structName}}Ctl import {{Lower .structName}}_rut
from tools.utils import register_scheduler{{if eq .isUser true}}, verify_token_format, analysis_token{{end}}

app = FastAPI()

app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

app.include_router({{Lower .structName}}_rut)

register_scheduler(app)

{{- if eq .isUser true}}
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    if request.url.path == "/auth/login" or request.url.path == "/docs" or request.url.path == "/openapi.json":
        return await call_next(request)

    tokenStr = request.headers.get("Authorization", None)
    tokenList = await verify_token_format(tokenStr)

    if type(tokenList) != list:
        return tokenList
    else:
        if await analysis_token(tokenList[1]):
            return await call_next(request)

    return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"code": 604, "message": "token错误", "data": None})
{{- end}}

